﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using HtmlAgilityPack;
using System.IO;
using HTMLTablePageScanner;

namespace t_HTMLTablePageScanner
{
    [TestClass]
    public class t_HTMLTextScanner
    {
        [TestMethod]
        public void TestSimpleWebPageForText()
        {
            FileInfo html = new FileInfo("..\\..\\..\\t_HTMLTablePageScanner\\TextScanTestsSimpleText.html");
            HtmlDocument h = new HtmlDocument();
            using (var r = html.OpenText())
            {
                h.LoadHtml(r.ReadToEnd());
            }

            string text = "";
            var result = h.DocumentNode.ScanForText(s => text = s, new string[] { "a" });
            Assert.AreEqual(0, result.Count(), "Expected nothing in this very simple file!");
        }
        [TestMethod]
        public void TestSimpleWebPageForTextAndLink()
        {
            FileInfo html = new FileInfo("..\\..\\..\\t_HTMLTablePageScanner\\TextScanTestsSimpleTextAndLink.html");
            HtmlDocument h = new HtmlDocument();
            using (var r = html.OpenText())
            {
                h.LoadHtml(r.ReadToEnd());
            }

            string text = "";
            var result = h.DocumentNode.ScanForText(s => text = s, new string[] { "a" });
            int count = 0;
            foreach (var item in result)
            {
                Assert.AreEqual("crap", text, "When we fired, expecting some text to be set!");
                Assert.AreEqual("a", item.Name, "Expected a link");
                Assert.AreEqual(0, count, "expected to go through this list of links just once!");
                count += 1;
            }
            Assert.AreEqual(count, 1, "expected one link!");

        }

        [TestMethod]
        public void TestSimpleWebPageForTextAndLinkInsideP()
        {
            FileInfo html = new FileInfo("..\\..\\..\\t_HTMLTablePageScanner\\TextScanTestsSimpleTextAndInnerLink.html");
            HtmlDocument h = new HtmlDocument();
            using (var r = html.OpenText())
            {
                h.LoadHtml(r.ReadToEnd());
            }

            string text = "";
            var result = h.DocumentNode.ScanForText(s => text = s, new string[] { "a" });
            int count = 0;
            foreach (var item in result)
            {
                Assert.AreEqual("crap", text, "When we fired, expecting some text to be set!");
                Assert.AreEqual("a", item.Name, "Expected a link");
                Assert.AreEqual(0, count, "expected to go through this list of links just once!");
                count += 1;
            }
            Assert.AreEqual(count, 1, "expected one link!");

        }

        [TestMethod]
        public void TestSimpleWebPageForTextAndLinkInsideBold()
        {
            FileInfo html = new FileInfo("..\\..\\..\\t_HTMLTablePageScanner\\TextScanTestsSimpleTextAndInnerBLink.html");
            HtmlDocument h = new HtmlDocument();
            using (var r = html.OpenText())
            {
                h.LoadHtml(r.ReadToEnd());
            }

            string text = "";
            var result = h.DocumentNode.ScanForText(s => text = s, new string[] { "a" });
            int count = 0;
            foreach (var item in result)
            {
                Assert.AreEqual("crap", text, "When we fired, expecting some text to be set!");
                Assert.AreEqual("a", item.Name, "Expected a link");
                Assert.AreEqual(0, count, "expected to go through this list of links just once!");
                count += 1;
            }
            Assert.AreEqual(count, 1, "expected one link!");

        }

        [TestMethod]
        public void TestSimpleWebPageForTextAndBurriedLink()
        {
            FileInfo html = new FileInfo("..\\..\\..\\t_HTMLTablePageScanner\\TextScanTestsSimpleTextAndBurriedLink.html");
            HtmlDocument h = new HtmlDocument();
            using (var r = html.OpenText())
            {
                h.LoadHtml(r.ReadToEnd());
            }

            string text = "";
            var result = h.DocumentNode.ScanForText(s => text = s, new string[] { "a" });
            int count = 0;
            foreach (var item in result)
            {
                Assert.IsTrue(text.Contains("upper limits") | text.Contains(".tex"), "When we fired, expecting some text to be set!");
                Assert.AreEqual("a", item.Name, "Expected a link");
                count += 1;
            }
            Assert.AreEqual(4, count, "expected one link!");
        }

        [TestMethod]
        public void TestDealingWithHTMLComments()
        {
            FileInfo html = new FileInfo("..\\..\\..\\t_HTMLTablePageScanner\\HTMLCommentInTheText.html");
            HtmlDocument h = new HtmlDocument();
            using (var r = html.OpenText())
            {
                h.LoadHtml(r.ReadToEnd());
            }

            string text = "";
            var result = h.DocumentNode.ScanForText(s => text = s, new string[] { "a" });
            int count = 0;
            foreach (var item in result)
            {
                Assert.IsFalse(text.Contains("<!--"), "Text contains the start of a html comment!");
                count += 1;
            }
            Assert.AreEqual(1, count, "expected one link!");
        }        
    }
}
